#coding=utf-8

import re

# 命名规范
pattern_name = "[A-Za-z_]\w*"

# 列约束条件
pattern_datatype = "(?:int|float|double|char|varchar|date|time)"            # 数据类型
pattern_size = "\([0-9]{1,3}\)"                                                 # 数据长度
pattern_column_constraint = "(?:NOT\s+NULL|PRIMARY\s+KEY)"    # 列属性约束

# 一列
pattern_column = pattern_name + "\s+" + pattern_datatype + "(?:\s*" + pattern_size + ")?" + "(?:\s*" + pattern_column_constraint + ")*"
# 多列
pattern_columns = "(?:" + pattern_column + "\s*,\s*)*" + pattern_column

# 可能会有多个主键作为表约束
pattern_keys = "(?:" + pattern_name + "\s*,\s*)*" + pattern_name

# 表约束，只实现了 PRIMARY KEY 和 FOREIGN KEY 约束
pattern_table_constraint = "(?:PRIMARY\s+KEY\s*\(\s*" + pattern_keys + "\s*\)|" + \
                           "FOREIGN\s+KEY\s*\(\s*" + pattern_name + "\s*\)\s*REFERENCES\s+" + pattern_name + "\s*\(\s*" + pattern_name + "\s*\)" + ")"
                           
# 最终拼起来的 CREATE 语句正则匹配表达式
pattern_create = "^CREATE\s+TABLE\s+(" + pattern_name + ")\s*\(\s*" + \
          "(" + pattern_columns + ")" + \
          "((?:\s*,\s*" + pattern_table_constraint + ")*)\s*\)\s*;*$"
                 

# 添加列
pattern_alter_add = "ADD\s+COLUMN\s+" + pattern_column
# 删除列
pattern_alter_drop = "DROP\s+COLUMN\s+" + pattern_name
# 修改列
pattern_alter_modify = "MODIFY\s+COLUMN\s+" + pattern_column
        
# 最终拼起来的 ALTER 语句        
pattern_alter = "^ALTER\s+TABLE\s+(" + pattern_name + ")\s+((?:" + pattern_alter_add + "|" + pattern_alter_drop + "|" + pattern_alter_modify + "))\s*;*$"
sql = raw_input("sql>:")
table = re.match(pattern_create,sql,re.I).group(2)
ta=table.split(',')[0]
print ta
t = re.match(pattern_column, ta, re.I).group()
print t
'''CREATE TABLE t1(id int not null primary key,name char(20));'''